"""The application's Globals object"""

from pylons import request

from beaker.cache import CacheManager
from beaker.util import parse_cache_config_options
from repoze.what.predicates import is_anonymous

class Globals(object):
    """Globals acts as a container for objects available throughout the
    life of the application

    """

    def __init__(self, config):
        """One instance of Globals is created during application
        initialization and is available during requests via the
        'app_globals' variable

        """
        self.cache = CacheManager(**parse_cache_config_options(config))
        
        self.user = lambda: request.environ.get('repoze.who.identity')['user']
        self.controller = lambda: request.environ.get('pylons.routes_dict')['controller']
        
        def get_app_name():
            controller = request.environ['pylons.controller']
            app_name_segments = controller.__module__.split('.')[2:-1]
            app_name = '.'.join(app_name_segments)
            return app_name
        
        self.app_name = lambda: get_app_name()
